package com.sunfeng.n6;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * 原子跟新器
 *
 * @author sunfeng
 * @date 2021/04/15 10:03
 **/
public class Test40 {
    public static void main(String[] args) {
        Student student = new Student();

        AtomicReferenceFieldUpdater<Student, String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");

        System.out.println(updater.compareAndSet(student, null, "张三"));
        System.out.println(student);

    }
}

class Student{
    /**
     * 这里必须使用volatile
     */
   volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
